<template>
    <div>
      <el-row style="height: 40px;">
          <el-col :span="4" style="text-align: left;">
              <h2 style="margin: 0;">物品借用审核</h2>
          </el-col>
          <el-col :span="8" :offset="12" style="text-align: right;">
              <el-button-group v-if="readonly == 'false'">
                  <el-button type="primary" @click="passApplication">通过</el-button>
                  <el-button type="primary" @click="unpassApplication">驳回</el-button>
              </el-button-group>
              &nbsp;&nbsp;
              <el-button type="primary" @click="reback">取消</el-button>
          </el-col>
      </el-row>
      <el-row>
          <el-col :span="20" :offset="2" style="text-align: center;">
              <el-form ref="form" :model="applicationForm" label-width="80px" class="demo-form-inline">
                  <el-row :gutter="20">
                      <el-col :span="12">
                          <el-form-item label="申请名称"> 
                              <el-input v-model="applicationForm.applicationName" readonly style="width: 100%"></el-input>
                          </el-form-item>
                      </el-col>
                      
                  </el-row>
                  <el-row :gutter="20">
                      <el-col :span="12">
                          <el-form-item label="申请人"> 
                              <el-input v-model="applicationForm.applicationEName" readonly></el-input>
                          </el-form-item>
                      </el-col>
                      <el-col :span="12">
                          <el-form-item label="申请部门"> 
                              <el-input v-model="applicationForm.dName" readonly></el-input>
                          </el-form-item>
                      </el-col>
                      
                  </el-row>
                  <el-row :gutter="20">
                      <el-col :span="12">
                          <el-form-item label="申请时间"> 
                              <el-input v-model="applicationForm.applicationTime" readonly></el-input>
                          </el-form-item>
                      </el-col>
                      <el-col :span="12">
                          <el-form-item label="借用类型">
                              <el-select v-model="applicationForm.borrowType" placeholder="请选择" style="width: 100%" disabled>
                                  <el-option
                                  v-for="item in borrowTypes"
                                  :key="item.value"
                                  :label="item.label"
                                  :value="item.value">
                                  </el-option>
                              </el-select>
                          </el-form-item>
                      </el-col>
                      
                      
                  </el-row>
                  <el-row :gutter="20">
                      <el-col :span="12">
                          <el-form-item label="借用仓库">
                              <el-select v-model="applicationForm.warehouseId" placeholder="请选择" style="width: 100%" disabled>
                                  <el-option
                                  v-for="item in warehouses"
                                  :key="item.warehouseId"
                                  :label="item.warehouseName"
                                  :value="item.warehouseId">
                                  </el-option>
                              </el-select>
                          </el-form-item>
                      </el-col>
                      <el-col :span="12">
                          <el-form-item label="仓管员"> 
                              <el-input v-model="warehouseManager" readonly></el-input>
                          </el-form-item>
                      </el-col>
                  </el-row>
                  <el-row>
                      <el-col :span="24">
                          <el-form-item label="备注"> 
                              <el-input type="textarea" v-model="applicationForm.comment" readonly></el-input>
                          </el-form-item>
                      </el-col>
                  </el-row>
              </el-form>
          </el-col>
      </el-row>
      <el-row>
          <el-col :span="4"> 
              <h3>借用列表</h3>
          </el-col>
          
      </el-row>
  
      <el-row>
          <el-col :span="16" :offset="4"> 
              <el-table
                  ref="multipleTable"
                  height="250px"
                  :data="applicationGoods"
                  tooltip-effect="dark"
                  style="width: 100%"
                  stripe
                  border
                  highlight-current-row
                  @selection-change="handleSelectionChange">
                  <el-table-column
                  type="selection"
                  align="center"
                  width="55">
                  </el-table-column>
                  <el-table-column
                  type="index"
                  label="序号"
                  align="center"
                  width="60">
                  </el-table-column>
                  <el-table-column
                  prop="warehouseName"
                  label="仓库名称"
                  align="center">
                  </el-table-column>
                  <el-table-column
                  prop="goodsName"
                  label="物资名称"
                  align="center">
                  </el-table-column>
                  <!-- 可编辑列 -->
                  <el-table-column
                  label="借用数量"
                  width="200"
                  align="center">
                  <template slot-scope="scope">
                    <el-input-number v-model="scope.row.brNum" :min="1" :max="scope.row.goodsNum" disabled></el-input-number>
                  </template>
                  </el-table-column>
                  </el-table>
          </el-col>
      </el-row>
      <Unpass :isShow="dialogVisable" @closeDialog="closeDialog" @saveBannedReason="saveReason"></Unpass>
    </div>
  </template>
  
  <script>
  import Unpass from '@/components/Unpass.vue';
  export default {
      name: 'AddorEditBApplication',
      components:{Unpass},
      data(){
          return{
              applicationForm:{
                  applicationId:0,
                  applicationName:'',
                  applicationEmpNo:0,
                  applicationEName:'',
                  deptno:0,
                  dName:'',
                  warehouseId:0,
                  warehouseName:'',
                  applicationTime:'',
                  comment:'',
                  borrowType:0,
                  checkType:0,
              },
              applicationGoods:[],
              warehouses:[],
              borrowTypes:[
                  {value:1,label:'个人借用'},
                  {value:2,label:'部门借用'},
                  {value:3,label:'项目借用'},
                  ],

              warehouseManager:"",

              user:{
                empNo:0,
                eName:"",
                role:0
              },
              readonly:false,
              dialogVisable: false,
              
          }
      },
      methods:{
          
        getWarehouse(){
            this.$http.get("/lc/queryWarehouse").then(res => {
                this.warehouses = res.data;
            }).catch(err => {
              console.error("Error getting warehouses:", err);
              this.$message.error("获取仓库信息失败: " + err.message);
            })
        },
          getWarehouseManager(){
            this.$http.get("/lc/queryHouseKeeperByHouseId?warehouseId="+this.applicationForm.warehouseId).then(res => {
                  // console.log(res.data);
                  let houseKeeperstr = "";
                  for (let emp of res.data){
                      houseKeeperstr += emp.ename+" ";
                  }
                  this.warehouseManager = houseKeeperstr;
              }).catch(error => { 
                console.log('未能正确获取仓库管理员信息')
              })
          },
          
          handleSelectionChange(val){
              
          },
          passApplication(){
            let saveData = {}
            if (this.user.role.indexOf('3') != -1){
                this.applicationForm.checkType = 3
            } 
            if (this.user.role.indexOf('5') != -1) {
                this.applicationForm.checkType = 4
            }
            this.applicationForm.auditEmpNo = this.user.empNo
            this.applicationForm.auditEName = this.user.eName
            
            let date = new Date()
            let dateStr = date.getFullYear()
            if (date.getMonth() < 9) {
                dateStr += "-0" + (date.getMonth() + 1)
            } else {
                dateStr += "-" + (date.getMonth() + 1)
            }
            if (date.getDate() < 10) {
                dateStr += "-0" + date.getDate()
            } else {
                dateStr += "-" + date.getDate()
            }
            this.applicationForm.checkTime = dateStr

            saveData.application = this.applicationForm
            saveData.applicationGoods = this.applicationGoods

            this.$http.post("/lc/updateApplicationStatus",saveData).then(res => { 
                // console.log(res.data)
                
                if (res.data == "success"){
                    this.$router.push({
                        path:"/checkBApplicationManager",
                        query:{
                            msg:"审核完成"
                        }
                    })
                } else{
                    this.$message.error("审核未成功")
                }
            }).catch(err => {
              console.error("Error passing application:", err);
              this.$message.error("审核通过操作失败: " + err.message);
            })
          },
          unpassApplication(){
            this.dialogVisable = true
          },
          reback(){
            this.$router.push({
                path:"/checkBApplicationManager"
            })
          },
          closeDialog(){
            this.dialogVisable = false
          },
          saveReason(val){
            this.dialogVisable = false
            this.applicationForm.bannedReason = val;

            let saveData = {}
            
            this.applicationForm.checkType = 1
            this.applicationForm.auditEmpNo = this.user.empNo
            this.applicationForm.auditEName = this.user.eName
            
            let date = new Date()
            let dateStr = date.getFullYear()
            if (date.getMonth() < 9) {
                dateStr += "-0" + (date.getMonth() + 1)
            } else {
                dateStr += "-" + (date.getMonth() + 1)
            }
            if (date.getDate() < 10) {
                dateStr += "-0" + date.getDate()
            } else {
                dateStr += "-" + date.getDate()
            }
            this.applicationForm.checkTime = dateStr

            saveData.application = this.applicationForm
            saveData.applicationGoods = this.applicationGoods
            // console.log(saveData)
            
            this.$http.post("/lc/updateApplicationStatus",saveData).then(res => { 
                // console.log(res.data)
                
                if (res.data == "success"){
                    this.$router.push({
                        path:"/checkBApplicationManager",
                        query:{
                            msg:"审核完成"
                        }
                    })
                } else{
                    this.$message.error("审核未成功")
                }
            }).catch(err => {
              console.error("Error rejecting application:", err);
              this.$message.error("驳回申请失败: " + err.message);
            })
          }
  
      },
      mounted(){
        this.user.eName = localStorage.getItem("ename");
        this.user.empNo = localStorage.getItem("empno");
        this.user.role = localStorage.getItem("roleid");
        if (this.$route.query.applicationInfo != null){
            this.applicationForm = this.$route.query.applicationInfo
        }
        if (this.$route.query.applicationGoods != null){
            this.applicationGoods = this.$route.query.applicationGoods
        }
        if (this.$route.query.readonly != null){
            this.readonly = this.$route.query.readonly
        }
        this.getWarehouse()
        this.getWarehouseManager()
      }
  }
  </script>
  
  <style>
  
  </style>